#include "temperature.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "view_common.h"
#include "Motor.h"
#include "view.h"
#include "times/view_times.h"
#include "times/view_time_0.h"
#include "sports_mode.h"
#include "app_trace.h"
#include "weather_ctrl.h"
#include "math.h"
#include "ct1711.h"
#include "times/view_time_11.h"
#include "times/view_time_8.h"
#include "temperature.inc"
#include "body_temperature.h"
#include "blecom.h"
#include "motor.h"
#define LOG(fmt, ...) //app_trace_log(fmt "\r\n", ##__VA_ARGS__)

static void show_temperature_float(int x, int y, float temperature, bool is_centigrade)
{
    const unsigned char *const *image_tab = num_12x29_TAB;
    int const font_width = image_tab[0][0] + (image_tab[0][1] << 8);
    int const font_height = image_tab[0][2] + (image_tab[0][3] << 8);
    int const interval = 3;
    int draw_x = 0;
    if (isnan(temperature))
    {
        x += 30;
        disp_draw_rect_color(x, y + font_height / 2 - 2, x + font_width - 1, y + font_height / 2, RGB565_COLOR_WHITE);
        x += font_width + interval;
        disp_draw_rect_color(x, y + font_height / 2 - 2, x + font_width - 1, y + font_height / 2, RGB565_COLOR_WHITE);
        return;
    }
    if (!is_centigrade)
    {
        temperature = 32.f + temperature * 1.8f;
    }
    int temperature_int = temperature * 100;
    if (temperature < 0)
    {
        disp_draw_rect_color(x, y + font_height / 2 - 1, x + font_width - 1, y + font_height / 2, RGB565_COLOR_WHITE);
        x += font_width + interval;
    }
    draw_x = disp_draw_user_image_num_filter_color(x, y, temperature_int / 100, font_width + interval, image_tab);
    disp_draw_rect_color(draw_x, y + font_height - 3, draw_x + 3 - 1, y + font_height - 1, RGB565_COLOR_WHITE);
    draw_x += 3 + interval;
    draw_x = disp_draw_user_image_num_with_digits_filter_color(draw_x, y, temperature_int % 100, 2, font_width + interval, image_tab);
    draw_x += 2;
    if (is_centigrade)
    {
        disp_draw_user_image_filter_color(draw_x, y, icon_centigrade);
    }
    else
    {
        disp_draw_user_image_filter_color(draw_x, y, icon_Fahrenheit);
    }
}

void view_temperature_display(int view)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 1000;
        switch (view)
        {
        case view_temperature_icon:
            break;
        case view_temperature_detail:
            if (disp_ctrl.auto_refresh_count <= BODY_TEMPERATURE_SECOND)
            {
                disp_ctrl.auto_close_delay = 2000 + DISP_CLOSE_TIME_DEFAULT;
            }
            break;
        }
        float temperature = ct1711_get_temperature();
        disp_ctrl.com_data.i_temp[21] = weather_get_temperature_type();
        disp_ctrl.com_data.f_temp[25] = get_wrist_temperature();
        disp_ctrl.com_data.f_temp[26] = get_body_temperature();
        disp_ctrl.com_data.i_temp[5] = ble_if_connected();
        disp_ctrl.com_data.i_temp[0] = get_manual_measure_remain_second();
    }
    datetime_t UIdatetime = DateSync();
    disp_ctrl.com_data.i_temp[3] = UIdatetime.time.hour;
    if (if_time_hour_system_12())
    {
        disp_ctrl.com_data.i_temp[3] %= 12;
        if (disp_ctrl.com_data.i_temp[3] == 0)
        {
            disp_ctrl.com_data.i_temp[3] = 12;
        }
    }
    disp_ctrl.com_data.i_temp[4] = UIdatetime.time.minute;
    disp_ctrl.com_data.i_temp[8] = battery_ctrl_if_charging();
    disp_ctrl.com_data.i_temp[9] = battery_ctrl_get_percent();
#if defined(SW07_PROJECT)
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 65, 16, FILE_IMG_hbb);
#else
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 60, 4, FILE_IMG_t501_tem_icon_60_4);
#endif
    if (disp_ctrl.com_data.i_temp[5])
    {
        disp_draw_user_image(42, 3, img_ble_connect);
    }
    else
    {
        disp_draw_user_image(42, 0, img_ble_disconnect);
    }

    show_common_time(disp_ctrl.com_data.i_temp[3], disp_ctrl.com_data.i_temp[4]);
    show_time_0_battery(disp_ctrl.com_data.i_temp[9], disp_ctrl.com_data.i_temp[8]);
#ifdef NO_WRIST_TEMPERATURE_UI
    disp_draw_user_image(107, 135, icon_body_temperature);
    if (isnan(disp_ctrl.com_data.f_temp[26]) && view == view_temperature_detail && disp_ctrl.auto_refresh_count % 2 == 0)
    {
    }
    else
    {
        show_temperature_float(78, 175, disp_ctrl.com_data.f_temp[26], disp_ctrl.com_data.i_temp[21] == WEATHER_TEMPERATURE_TYPE_C);
    }
#else
    disp_draw_user_image(47, 138, icon_wrist_temperature);
    disp_draw_user_image(168, 135, icon_body_temperature);

    show_temperature_float(14, 175, disp_ctrl.com_data.f_temp[25], disp_ctrl.com_data.i_temp[21] == WEATHER_TEMPERATURE_TYPE_C);
    if (isnan(disp_ctrl.com_data.f_temp[26]) && view == view_temperature_detail && disp_ctrl.auto_refresh_count % 2 == 0)
    {
    }
    else
    {
        show_temperature_float(138, 175, disp_ctrl.com_data.f_temp[26], disp_ctrl.com_data.i_temp[21] == WEATHER_TEMPERATURE_TYPE_C);
    }
    disp_draw_rect_color(120, 175, 120, 175 + 30, 0x630c);
#endif

    switch (view)
    {
    case view_temperature_icon:
        disp_show_localize(DISP_LFS_FILE_INDEX_3, localize_string_key_StartBodyTemperatureTips, 51);
        break;
    case view_temperature_detail:
        disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_MeasuringBodyTemperatureTips, 50);
        disp_draw_user_image_num_filter_color(136, 212, disp_ctrl.com_data.i_temp[0], 12, num_9x23_TAB);
        break;
    }
}

view_event_result_t view_temperature_detail_event(event_param_t const *param)
{
    switch (param->event)
    {
    case event_view_entry:
        LOG("view_temperature_event_view_entry");
        start_manual_measure(view_temperature_icon);
        // start_body_measure(BODY_TEMPERATURE_SECOND, true, view_temperature_icon);
        if (param->previous_view == view_prompt_hyperthemia || param->previous_view == view_prompt_not_wear)
        {
            LOG("view_temperature_event_view_entry");
            display_show(view_temperature_icon, DISP_MODE_NORMAL);
        }
        return view_event_result_handled;
    case event_view_exit:
        LOG("view_temperature_event_view_exit");
        // stop_body_measure();
        stop_manual_measure();
        app_cmd_send_ack(SmartBand_temperature_exit, 0);
        return view_event_result_handled;
#ifdef EVENT_LEVEL_UP
    case event_move_level_up:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
#ifdef USE_TOUCHPAD
    case BUTTON_EVENT_TP_TAP_LONG:
#endif
        display_show(view_temperature_icon, DISP_MODE_NORMAL);
        return view_event_result_handled;
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
#endif
#ifdef USE_TOUCHPAD
    case BUTTON_EVENT_TP_TAP:
#endif
#ifdef EVENT_PREVIOUS_VIEW
    case event_move_previous_view:
#endif
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
#endif
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
view_event_result_t view_temperature_icon_event(event_param_t const *param)
{
    switch (param->event)
    {
    case event_view_entry:
        ct1711_start();
        return view_event_result_handled;
    case event_view_exit:
        ct1711_stop();
        return view_event_result_handled;
#ifdef USE_TOUCHPAD
    case BUTTON_EVENT_TP_TAP_LONG:
        display_show(view_temperature_detail, DISP_MODE_NORMAL);
        return view_event_result_handled;
    case BUTTON_EVENT_TP_TAP:
        return view_event_result_handled;
#endif
    }
    return view_event_result_not_handled;
}
view_event_result_t view_temperature_event(int view, event_param_t const *param)
{
    switch (view)
    {
    case view_temperature_detail:
        return view_temperature_detail_event(param);
    case view_temperature_icon:
        return view_temperature_icon_event(param);
    }
    return view_event_result_not_handled;
}
